<?php

namespace app\api\controller\web;

use addons\qrcode\Qrcode;
use app\admin\model\apply\Carousel;
use app\admin\model\apply\Confg;
use app\admin\model\apply\Notice;
use app\admin\model\nursery\NurserySell;
use app\admin\model\User;
use app\common\controller\Api;
use app\common\helpers\ImageUtil;
use app\common\library\Auth;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Invite;
use app\common\model\MoneyLog;
use app\common\model\user\Receive;
use app\common\model\Withdraw;
use app\common\model\WxData;
use fast\Random;
use think\Config;
use think\Db;
use think\Exception;
use think\Model;
use think\Validate;

/**
 *  首页接口
 */
class Home extends Api
{
    protected $noNeedLogin = ['carouselList','userTopList','noticeNew','noticeList','nurserySellTopList'];
    protected $noNeedRight = '*';
    protected $model ='';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 轮播图
     */
    public function CarouselList()
    {
        $list = (new Carousel())
            ->order('sort desc')
//            ->limit(5)
            ->select();
        if($list){
            foreach ($list as $v){
                $v['img'] = ImageUtil::handle($v['img'],'small');
            }
        }
        $this->success("成功", $list);
    }

    /**
     * 最新公告通知
     */
    public function noticeNew()
    {
        $info = (new Notice())
            ->order('sort,createtime desc')
            ->find();
        $this->success("成功", $info);
    }


    /**
     * 公告通知列表
     */
    public function noticeList()
    {
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        $list = (new Notice())
            ->order('sort,createtime desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);
//        $result = array("total" => $list->total(), "list" => $list->items());
        $this->success("成功", $list);
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 供应商列表
     */
    public function UserTopList()
    {
        $list = (new User())
            ->field('id,avatar,level')
            ->where(['status'=>'normal'])
            ->where(['level'=>['>',0]])
//            ->order('level desc')
            ->orderRaw('rand()')
            ->limit(60)
            ->cache('UserTopList',3600)
            ->select();
        if($list){
            foreach ($list  as $v){
                $v['avatar'] =ImageUtil::handle($v['avatar']);
            }
        }
        $this->success("成功", $list);
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 供应发布top
     */
    public function nurserySellTopList()
    {
        $info = Confg::get(1);
        $list = (new NurserySell())->alias('ns')
            ->field('ns.user_id,u.avatar,ns.name,ns.price,refresh_time')
            ->join('user u','ns.user_id = u.id','left')
            ->order('refresh_time desc')
            ->where('refresh_time','>=',date('Y-m-d H:i:s',strtotime('- '.$info['near_hour'].' hour',time())))
           ->select();
        if($list){
            foreach ($list  as $v){
                $time = time() - strtotime($v['refresh_time']);
                if (floor($time / 86400) > 0) {
                    $string = floor($time / 86400).'天前';
                }else{
                    if (floor($time/ 3600) > 0) {
                        $string = floor($time/ 3600 ).'小时前';
                    }else{
                        if (floor($time/ 60 ) > 0) {
                            $string = floor($time/ 60 ).'分钟前';

                        }else{
                            $string = floor($time % 60).'秒前';
                        }
                    }
                }
                $v['string'] = $string;
                $v['avatar'] = ImageUtil::handle($v['avatar']);
            }
        }
        $this->success("成功", $list);
    }


}
